//
//  PrimerImgCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/21.
//  Copyright © 2015年 game. All rights reserved.
//

#import "PrimerImgCell.h"
#import "CollectionViewCell.h"//与首页公用一个collectionview
#import "UIImageView+WebCache.h"
#import "NewestAticleCtr.h"
#import "InTroModel.h"
@interface PrimerImgCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UIView *lin1;
@property (nonatomic,strong) UIView *lin2;
@end
@implementation PrimerImgCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    }
    return self;
}

-(void)setDataAry:(NSArray *)DataAry{
    _DataAry = DataAry;
    
    [self CreatColletion];
}

-(void)CreatColletion{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    CGFloat yd = (SCREEN_HEIGHT*0.4)*0.03;//竖间隙；
    CGFloat xd = SCREEN_WIDTH/6;//横间隙
    CGFloat h = SCREEN_HEIGHT*0.45;//collection高度
    
    layout.itemSize = CGSizeMake(xd+10,(h-yd*4)/2);
    
    if (kDIEIsIPhone4SOrLess) {
         layout.sectionInset = UIEdgeInsetsMake(yd+10, xd, yd, xd);
        layout.minimumLineSpacing = yd;   //最小竖间距

    }else{
        layout.sectionInset = UIEdgeInsetsMake(yd+5, xd, yd, xd);
        layout.minimumLineSpacing = yd+12;  //最小竖间距
    }
        //最小行间距
    layout.minimumInteritemSpacing = xd;
    
    self.collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,h) collectionViewLayout:layout];
    self.collectionview.scrollEnabled = NO;//让collectionview不能滚动
    self.collectionview.delegate=self;
    self.collectionview.dataSource=self;
    [self.collectionview registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectionview.backgroundColor= [UIColor whiteColor];
    [self.contentView addSubview:self.collectionview];
    
    //竖线
    self.lin1= [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-1,0, 0.5, h)];
    self.lin1.backgroundColor = [UIColor lightGrayColor];
    self.lin1.alpha = 0;
    [self.collectionview addSubview:self.lin1];
    
    //横线
    self.lin2 = [[UIView alloc]initWithFrame:CGRectMake(0,(h)/2+2, SCREEN_WIDTH,0.5 )];
    self.lin2.backgroundColor = [UIColor lightGrayColor];
    self.lin2.alpha = 0;
    [self.collectionview addSubview:self.lin2];
}

#pragma mark--Count
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.DataAry.count!=0) {
        self.lin1.alpha = 0.4;
        self.lin2.alpha = 0.4;
    }
    return self.DataAry.count;
}

#pragma mark--DataSource
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell"  forIndexPath:indexPath];
    
    InTroModel *model = self.DataAry[indexPath.item];
    
    NSString *imageUrl;
    if (kDIEIsIPhone6P) {
        imageUrl = model.icon_3x;
    }else{
        imageUrl = model.icon_2x;
    }
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
    cell.title.text = model.title;

    cell.title.font = font(SCREEN_WIDTH*0.045);
  
       return cell;
}

#pragma mark---Click
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    InTroModel *mode = self.DataAry[indexPath.item];
    if (_Present) {
        _Present(mode.typeid,mode.title);
    }
}

@end